import logging
import optparse
import sys

from orioluspith.packer import version
from orioluspith.packer.packer import Packer

def exit(parser):
    parser.print_help()
    parser.destroy()
    sys.exit(2)
    
def run():
    usage = """usage: oripack [options] path"""

    parser = optparse.OptionParser(usage=usage, version = "oripack %s" % version)
    
#    parser.add_option("-k", "--key-id", dest = "key_id", help = "identificator of a key that will be used for sing packet")
    parser.add_option("-n", "--no-sign", dest = "no_sign", action = "store_false", default = True, help = "option that disable signing packet")
    parser.add_option("-r", "--keyring", dest = "keyring", help = "path to keyring")
    parser.add_option("-s", "--secret-keyring", dest = "secret_keyring", help = "path to secret keyring")
    parser.add_option("-p", "--key-password", dest = "password", help = "password for default key into secret_keyring")
    parser.add_option("-o", "--output-path", dest = "output_path", help = "path to output directory")
    
    (options, args) = parser.parse_args()
    
    logging.basicConfig(level = logging.INFO, format = "[%(levelname)s] %(message)s", )

    if len(args) != 1:
        exit(parser)
    
    if options.output_path:
        output_path = options.output_path
    else:
        output_path = "."
        
    keyring = None
    if options.keyring:
        keyring = options.keyring

    secret_keyring = None
    if options.secret_keyring:
        secret_keyring = options.secret_keyring

    password = None
    if options.password:
        password = options.password
        
    if options.no_sign:
        packer = Packer(output_path = output_path, keyring = keyring)
        if packer.pack_and_sign(args[0], password, secret_keyring):
            print "Packet was successfully created and signed."
        else:
            print "Error occured while packet was creating and signing."
    else:    
        packer = Packer(output_path = output_path)
        packet_name = packer.pack(args[0])
        if packet_name:
            print "Packet %s was successfully created." % packet_name
        else:
            print "Error occured while packet was creating."

if __name__ == "__main__":
    run()
